package com.tiancikeji.zaoke.app.service.zone.impl;

import java.util.Set;

import com.tiancikeji.zaoke.app.dao.zone.ZoneDao;
import com.tiancikeji.zaoke.app.model.zone.Zone;
import com.tiancikeji.zaoke.app.service.zone.ZoneService;
import com.tiancikeji.zaoke.app.util.LocalCaches;

@SuppressWarnings("unchecked")
public class ZoneServiceImpl implements ZoneService{
	
	private ZoneDao zoneDao;
	
	private long expireTime;

	public Zone getZoneById(long zoneId) {
		Set<Zone> zones = getAllZones();
		
		if(zones != null && zones.size() > 0)
			for(Zone zone : zones)
				if(zone.getId() == zoneId)
					return zone;
		
		return null;
	}

	public Set<Zone> getAllZones(){
		Set<Zone> zones = (Set<Zone>)LocalCaches.get(LocalCaches.ZAOKE_CACHE_ZONES_KEY, System.currentTimeMillis(), expireTime);
		
		if(zones == null){
			zones = zoneDao.getZonesByState(new Integer[]{Zone.STATE_ACTIVE});
			
			if(zones != null && zones.size() > 0)
				LocalCaches.set(LocalCaches.ZAOKE_CACHE_ZONES_KEY, zones, System.currentTimeMillis());
		}
		
		return zones;
	}

	public long getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(long expireTime) {
		this.expireTime = expireTime;
	}

	public ZoneDao getZoneDao() {
		return zoneDao;
	}

	public void setZoneDao(ZoneDao zoneDao) {
		this.zoneDao = zoneDao;
	}
}
